#!/usr/bin/env python

import unittest
from network import NetWork
from httputil import HttpUtil

class TestNetWork(NetWork, unittest.TestCase):
  def __init__(self, *args, **kwargs):
    unittest.TestCase.__init__(self, *args, **kwargs)
    HttpUtil.__init__(self)

  def setUp(self):
    #print "setUp"
    pass

  def tearDown(self):
    #print "tearDown"
    pass
  
  def test_zone(self):
    (id, ) = self.createZone("basic", "testzone", "172.16.236.32", "172.16.236.32")
    self.assertTrue(id > 0)
    (count, ) = self.listZones(id)
    self.assertTrue(count > 0)
    (success, ) = self.deleteZone(id)
    self.assertEqual(success, "true")

  def test_pod(self):
    (zoneid, ) = self.createZone("basic", "testzone1", "172.16.236.32", "172.16.236.32")
    self.assertTrue(zoneid > 0)
    (id, ) = self.createPod(zoneid, "testpod20", "255.255.255.0", "172.16.236.141", "172.16.236.149", "172.16.236.254")
    self.assertTrue(id > 0)
    (count, ) = self.listPods(id)
    self.assertTrue(count > 0)
    (success, ) = self.deletePod(id)
    self.assertEqual(success, "true")
    (success, ) = self.deleteZone(zoneid)
    self.assertEqual(success, "true")

  def test_cluster(self):
    (zoneid, ) = self.createZone("basic", "testzone5", "172.16.236.32", "172.16.236.32")
    self.assertTrue(zoneid > 0)
    (podid, ) = self.createPod(zoneid, "testpod20", "255.255.255.0", "172.16.236.141", "172.16.236.149", "172.16.236.254")
    self.assertTrue(podid > 0)
    (id, ) = self.addCluster(zoneid, podid, "testcluster5", "CloudManaged", "XenServer")
    self.assertTrue(id > 0)
    (count, ) = self.listClusters(id)
    self.assertTrue(count > 0)
    (success, ) = self.deleteCluster(id)
    self.assertEqual(success, "true")
    (success, ) = self.deletePod(podid)
    self.assertEqual(success, "true")    
    (success, ) = self.deleteZone(zoneid)
    self.assertEqual(success, "true")

  def test_host(self):
    (zoneid, ) = self.createZone("basic", "testzone24", "172.16.236.32", "172.16.236.32")
    self.assertTrue(zoneid > 0)
    (podid, ) = self.createPod(zoneid, "testpod24", "255.255.255.0", "172.16.236.141", "172.16.236.149", "172.16.236.254")
    self.assertTrue(podid > 0)
    (clusterid, ) = self.addCluster(zoneid, podid, "testcluster24", "CloudManaged", "XenServer")
    self.assertTrue(clusterid > 0)
    (id, ) = self.addHost(zoneid, podid, clusterid, "root", "redhat", "http://172.16.236.27", "XenServer")
    self.assertTrue(id > 0)
    (count, ) = self.listHosts(id)
    self.assertTrue(count > 0)
    (state, ) = self.prepareHostForMaintenance(id)
    self.assertEqual(state, "Maintenance")   
    (success, ) = self.deleteHost(id)
    self.assertEqual(success, "true")
    (success, ) = self.deleteCluster(clusterid)
    self.assertEqual(success, "true")     
    (success, ) = self.deletePod(podid)
    self.assertEqual(success, "true")    
    (success, ) = self.deleteZone(zoneid)
    self.assertEqual(success, "true")

  def test_ostype(self):
    (count, ) = self.listOsTypes(1)
    self.assertTrue(count > 0)

  def test_account(self):
    (id, ) = self.createAccount("test3", "202cb962ac59075b964b07152d234b70", "test@123.com", "first", "last", 1, 1)
    self.assertTrue(id > 0)
    (count, ) = self.listAccounts(id)
    self.assertTrue(count > 0)
    (success, ) = self.deleteAccount(id)
    self.assertEqual(success, True)

  def test_vlanIpRange(self):
    (zoneid, ) = self.createZone("basic", "testzone30", "172.16.236.32", "172.16.236.32")
    self.assertTrue(zoneid > 0)
    (podid, ) = self.createPod(zoneid, "testpod30", "255.255.255.0", "172.16.236.141", "172.16.236.149", "172.16.236.254")
    self.assertTrue(podid > 0)
    (id, ) = self.createVlanIpRange(zoneid, podid, "untagged", "false", "172.16.236.141", "172.16.236.149", "255.255.255.0", "172.16.236.254")
    self.assertTrue(id > 0)
    (count, ) = self.listVlanIpRanges(zoneid, podid, "untagged")
    self.assertTrue(count > 0)
    (success, ) = self.deleteVlanIpRange(id)
    self.assertEqual(success, "true")    
    (success, ) = self.deletePod(podid)
    self.assertEqual(success, "true")    
    (success, ) = self.deleteZone(zoneid)
    self.assertEqual(success, "true")

  def test_securityGroup(self):
    (accountid, ) = self.createAccount("testaccount", "202cb962ac59075b964b07152d234b70", "test@123.com", "first", "last", 1, 1)
    self.assertTrue(accountid > 0)
    (securityGroupId, ) = self.createSecurityGroup("testSecurityGroup", "testaccount", 1)
    self.assertTrue(securityGroupId > 0)
    (count, ) = self.listSecurityGroups(securityGroupId)
    self.assertTrue(count > 0)
    (id, ) = self.authorizeSecurityGroupIngress(securityGroupId, "TCP", 1111, 2222, "172.16.236.142/149")
    self.assertTrue(id > 0)
    (success, ) = self.revokeSecurityGroupIngress(id)
    self.assertEqual(success, True)
    (success, ) = self.deleteSecurityGroup(securityGroupId)
    self.assertEqual(success, "true")
    (success, ) = self.deleteAccount(accountid)
    self.assertEqual(success, True)    

def get_suite():
  suite = unittest.TestSuite()
  #suite.addTest(TestNetWork("test_zone"))
  #suite.addTest(TestNetWork("test_pod"))
  #suite.addTest(TestNetWork("test_cluster"))
  #suite.addTest(TestNetWork("test_host"))
  #suite.addTest(TestNetWork("test_ostype"))
  #suite.addTest(TestNetWork("test_account"))
  #suite.addTest(TestNetWork("test_vlanIpRange"))
  suite.addTest(TestNetWork("test_securityGroup"))
  return suite

if __name__ == "__main__":
  suite = get_suite()
  runner = unittest.TextTestRunner()
  runner.run(suite)
